/**
 * 商品相关api
 */
import {
	http
} from '@/api/config.js'

import '@/utils/userActionUtil.js'

/**
 * 查询用户商品分页列表
 * @param {Object} params - 用户id
 */
export const getProductByUser = (params) => {
	return http.post('/product/user', {
		...params
	})
}

/**
 * 查询商品详情
 * @param {Object} params - 查询参数
 */
export const getProductInfo = (id) => {
	return http.get('/product/info/' + id)
}

/**
 * 获取商品下载路径
 * @param {Object} id - 商品ID
 */
export const getProductDownloadUrl = (id) => {
	return http.get('/product/download/' + id).then(res => {
		uni.showModal({
			title: '复制链接下载，15分钟有效期',
			content: res.data,
			showCancel: true,
			cancelText: '取消',
			confirmText: '立即复制',
			success: showRes => {
				if (showRes.confirm) {
					uni.setClipboardData({
						data: res.data,
						success: () => {
							uni.showToast({
								title: "复制成功"
							})
						}
					});
				}
			}
		});
	})
}

/**
 * 搜索商品
 * @param {Object} params - 查询参数
 */
export const getSearchProductPage = (params) => {
	return http.post('/product/search/', {
		...params
	})
}

/**
 * 收藏/取消收藏商品
 * @param {Object} id - 商品ID
 * @param {Object} status - 0取消收藏 1收藏
 */
export const likeProductById = (id, status) => {
	const response = http.get('/product/like/' + id + '/' + status);

	response.then(res => {
		uni.showToast({
			title: status === 0 ? "已取消收藏" : "收藏成功",
			duration: 500
		})
	})

	return response;
}

/**
 * 根据分类等级和ID查询分类商品
 * @param {Object} params - 查询参数
 */
export const getHomeWaterfallsFlowData = (params) => {
	return http.post('/product/page', {
		...params
	})
}

/**
 * 查询收藏记录
 * @param {Object} params - 查询参数
 */
export const getProductLikePage = (params) => {
	return http.post('/product/like/page', {
		...params
	})
}

/**
 * 查询下载记录
 * @param {Object} params - 查询参数
 */
export const getProductDownloadPage = (params) => {
	return http.post('/product/download/page', {
		...params
	})
}

/**
 * 查询浏览记录
 * @param {Object} params - 查询参数
 */
export const getProductViewPage = (params) => {
	return http.post('/product/view/page', {
		...params
	})
}

/**
 * 删除浏览记录
 * @param {Object} ids - 记录ID
 */
export const deleteProductView = (ids) => {
	return http.post('/product/view/delete', ids)
}
/**
 * 删除下载记录
 * @param {Object} ids - 记录ID
 */
export const deleteProductDownload = (ids) => {
	return http.post('/product/download/delete', ids)
}
/**
 * 删除收藏记录
 * @param {Object} ids - 记录
 */
export const deleteProductLike = (ids) => {
	return http.post('/product/like/delete', ids)
}